package com.abbas.smartCity.util;

import android.app.ProgressDialog;
import android.util.Log;

import androidx.activity.ComponentActivity;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

import java.util.Objects;

/**
 * 本类无需实现
 *
 * 弹窗管理器
 */
@NoImpl
public class DialogManager implements LifecycleObserver {

    private ProgressDialog progressDialog = null;//弹窗
    private static DialogManager dialogManager;//单例模式

    /**
     * 禁止new出对象。最好的方法应该是公开此构造，并在构造当中抛出异常。私有化空构造是不合理的。
     */
    private DialogManager() {
    }

    //懒汉式单例
    public static DialogManager getInstance() {
        if (dialogManager == null) {
            synchronized (DialogManager.class) {
                if (dialogManager == null) {
                    dialogManager = new DialogManager();
                }
            }
        }
        return dialogManager;
    }

    /**
     * 获取一个弹窗
     * @return 不可取消的非精确进度弹窗
     */
    public ProgressDialog getDialog() {
        if (progressDialog == null) {
            final ComponentActivity activity = ActivityManager.getInstance().get();
            final Lifecycle lifecycle = activity.getLifecycle();
            lifecycle.addObserver(this);
            progressDialog = new ProgressDialog(activity);
            progressDialog.setCancelable(false);
            Log.e("DialogManager","弹窗创建...");
            progressDialog.setTitle("加载中");
            progressDialog.setMessage("请稍等...");
        }
        return progressDialog;
    }

    /**
     *
     * @param max 最终进度
     * @param now 当前进度
     * @return 不可取消的精确进度弹窗
     */
    public ProgressDialog getDialog(int max,int now){
        if (progressDialog == null) {
            final ComponentActivity activity = ActivityManager.getInstance().get();
            final Lifecycle lifecycle = activity.getLifecycle();
            lifecycle.addObserver(this);
            progressDialog = new ProgressDialog(activity);
            progressDialog.setCancelable(false);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDialog.setMax(max);
            progressDialog.setProgress(now);
            Log.e("DialogManager","弹窗创建...");
            progressDialog.setTitle("加载中");
            progressDialog.setMessage("请稍等...");
        }
        return progressDialog;
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    private void onContextDestroy() {
        progressDialog = null;
        Log.e("DialogManager","弹窗销毁...");
    }


}
